﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.ComponentModel;
using System.Data.SqlClient;

namespace WinLogicUniversity
{
    public partial class frmAfterDeliveryDetail : Form
    {
        LogicUniversityT2Entities entity = new LogicUniversityT2Entities();

        public frmAfterDeliveryDetail()
        {
            InitializeComponent();
        }

        private void dgdeliverydetail_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            lblItemID.Text = dgdeliverydetail.CurrentRow.Cells[0].Value.ToString();
            lblRequestedQuantity.Text = dgdeliverydetail.CurrentRow.Cells[2].Value.ToString();
            //txtDeliveredQty.Text = dgdeliverydetail.CurrentRow.Cells[3].Value.ToString();
            lblDeptReqID.Text = dgdeliverydetail.CurrentRow.Cells[4].Value.ToString();
            lblDepartmentID.Text = dgdeliverydetail.CurrentRow.Cells[5].Value.ToString();
            int aftdeliver = Convert.ToInt32(dgdeliverydetail.CurrentRow.Cells[3].Value.ToString());
            int allocateqty = Convert.ToInt32(dgdeliverydetail.CurrentRow.Cells[6].Value.ToString());
            int reqqty = Convert.ToInt32(lblRequestedQuantity.Text);
            txtDelivered.Text = Convert.ToString(allocateqty - aftdeliver);
            txtBalance.Text = Convert.ToString(reqqty - allocateqty);


            errorProvider1.Clear();
            txtBrokenItem.Text = "";
            txtDeliveredQty.Text = ""; 
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Boolean myvalidate;
            myvalidate = MyValidation();
            try
            {
                if (myvalidate == true)
                {
                    int i = Convert.ToInt32(lblRequestedQuantity.Text);
                    int j = Convert.ToInt32(txtDeliveredQty.Text);
                    int k = Convert.ToInt32(txtBrokenItem.Text);

                    DepartmentRequisition DepReqUpdate = entity.DepartmentRequisitions.First(ite => ite.DeptRequisitionID == lblDeptReqID.Text);
                    AdjustmentVoucher AdjVoucher = new AdjustmentVoucher();

                    if (DepReqUpdate.AfterDeliveredQuantity == 0)
                    {
                        DepReqUpdate.AfterDeliveredQuantity = Convert.ToInt32(txtDeliveredQty.Text);
                    }
                    else
                    {
                        DepReqUpdate.AfterDeliveredQuantity += Convert.ToInt32(txtDeliveredQty.Text);
                        DepReqUpdate.OutstandingDeliveredQty = Convert.ToInt32(txtDeliveredQty.Text);
                    }

                    DepReqUpdate.AllocatedQuantity -= Convert.ToInt32(txtBrokenItem.Text);

                    if (DepReqUpdate.RequestedQuantity == DepReqUpdate.AfterDeliveredQuantity)
                    {
                        DepReqUpdate.Status = "D";
                    }
                    else if (DepReqUpdate.RequestedQuantity != DepReqUpdate.AfterDeliveredQuantity)
                    {
                        DepReqUpdate.Status = "O";
                    }
                    else
                    {
                        DepReqUpdate.Status = "NR";
                    }

                    if (txtBrokenItem.Text != "0")
                    {
                        AdjVoucher.AdjustmentID = DBBroker.AfterDeliveryBroker.AutoGenAdjVouID();
                        AdjVoucher.ItemID = lblItemID.Text;
                        //AdjVoucher.Date = Convert.ToDateTime(txtDate.Text);                        
                        AdjVoucher.Date = Convert.ToDateTime(DateTime.Now);
                        int broken = Convert.ToInt32(txtBrokenItem.Text);
                        int brokenitem = 0 - broken;
                        AdjVoucher.Quantity = Convert.ToInt32(brokenitem);
                        AdjVoucher.Status = "Pending";
                        AdjVoucher.Remarks = "Broken";
                        entity.AddToAdjustmentVouchers(AdjVoucher);
                        entity.SaveChanges();
                    }


                    entity.SaveChanges();
                    bindgridview();
                    txtDeliveredQty.Text = "";
                    txtBrokenItem.Text = "";

                    if (dgdeliverydetail.RowCount == 0)
                    {                        
                        txtStatus.Text = "Delivered";
                    }
                    else
                    {                        
                        txtStatus.Text = "Outstanding";
                    }

                }
            }
            catch (SqlException ex)
            {
                MyMessageBox.ShowBox("SQL Error " + ex.Number + ":" + ex.Message);
            }
        }
        private void bindgridview()
        {
            dgdeliverydetail.DataSource = DBBroker.AfterDeliveryBroker.getDepartmentReqList();
            dgdeliverydetail.Columns[3].Visible = false;
            dgdeliverydetail.Columns[4].Visible = false;
            dgdeliverydetail.Columns[5].Visible = false;
            dgdeliverydetail.Columns[6].Visible = false;


        }

        #region Validation
        private Boolean MyValidation()
        {
            errorProvider1.Clear();

            if (lblDepartmentID.Text == "")
            {
                errorProvider1.SetError(dgdeliverydetail, "Please Select Row");
                return false;
            }
            else if (txtBrokenItem.Text.Trim() == "" || DBBroker.Validation.IsNaturalNumber(txtBrokenItem.Text) == false)
            {
                errorProvider1.SetError(txtBrokenItem, "Please Type Valid Broken Item");
                return false;
            }
            else if (txtDeliveredQty.Text.Trim() == "" || DBBroker.Validation.IsNaturalNumber(txtDeliveredQty.Text) == false || txtDeliveredQty.Text == "0")
            {
                errorProvider1.SetError(txtDeliveredQty, "Please Type Valid Delivered Quantity");
                return false;
            }

            return true;

        }
        private void txtBrokenItem_Validated(object sender, EventArgs e)
        {
            if (DBBroker.Validation.IsNaturalNumber(txtBrokenItem.Text) == true || txtBrokenItem.Text != "")
            {
                errorProvider1.Clear();
            }

        }

        private void txtDeliveredQty_Validated(object sender, EventArgs e)
        {
            if (DBBroker.Validation.IsNaturalNumber(txtDeliveredQty.Text) == true || txtDeliveredQty.Text != "")
            {
                errorProvider1.Clear();
            }
        }


        #endregion 

        private void frmAfterDeliveryDetail_Load(object sender, EventArgs e)
        {
            var DepartmentData = entity.Departments;
            cboDepartmentCode.DataSource = DepartmentData;
            cboDepartmentCode.DisplayMember = "DepartmentID";        
        }

        private void cboDepartmentCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            string dID = cboDepartmentCode.Text;
            DBBroker.AfterDeliveryBroker departmentID = new DBBroker.AfterDeliveryBroker(dID);
            bindgridview(); 
        }
    }
}
